<?php
require('../config.php');

		$sid=$_REQUEST['sid'];
		$qishu=$_REQUEST['qishu'];
		$q_uid=-1;
		//获取期数
		$sql="select s.qishu from `".$oto."_shoplist` as s where sid='{$sid}' order by qishu desc limit 1";
		$result=$db->query($sql);
		while($row=$result->fetch_assoc()){
			$qishu_info=$row;
		}
		//获取商品基本参数
		$sql="select s.id,s.sid,s.shop_id,s.title,s.qishu,s.thumb,s.canyurenshu,s.zongrenshu,s.shenyurenshu,s.yunjiage,s.money,s.q_end_time,s.q_showtime,q_uid,q_user_code,s.picarr,s.yunjiage from `".$oto."_shoplist` as s where sid='{$sid}' and qishu='{$qishu}'";
		$result=$db->query($sql);
		while($row=$result->fetch_assoc()){
			$dq_shoplist=$row;
		}
		if(isset($dq_shoplist)) 
		{
			$data['content'][0]=$dq_shoplist;
			if($data['content'][0]['q_end_time']==null) $data['content'][0]['q_end_time']="";
			if($data['content'][0]['q_uid']==null) $data['content'][0]['q_uid']="";
			if($data['content'][0]['q_user_code']==null) $data['content'][0]['q_user_code']="";
			$data['content'][0]['dq_time']=time();
			$data['content'][0]['img_list']=explode(',',$dq_shoplist['picarr']);
			for($i=0;$i<count($data['content'][0]['img_list']);$i++)
			{
				$lin_ctu[$i]=explode('@',$data['content'][0]['img_list'][$i]);
				if(count($lin_ctu[$i])>1) $data['content'][0]['img_list'][$i]=$lin_ctu[$i][0];
			}
			$data['content'][0]['picarr']="";
			$q_uid=$dq_shoplist['q_uid'];
		}
		else $data['content']=[];
		if(isset($qishu_info)) $data['content'][0]['max_qishu']=$qishu_info['qishu'];
		//获取当前期数中奖者信息
		if($dq_shoplist['q_end_time']!=null && $dq_shoplist['q_showtime']='N')
		{
			$sql="select sum(`gonumber`) as num,`time`,username,uphoto,ip from `".$oto."_member_go_record` as r where shopid='".$dq_shoplist['id']."' and uid='{$q_uid}' order by `time` desc";
			$result=$db->query($sql);
			while($row=$result->fetch_assoc()){
				$record[]=$row;
			}
			$sql="select * from `".$oto."_users` as r where userId='{$q_uid}'";
			$result=$db->query($sql);
			while($row=$result->fetch_assoc()){
				$users=$row;
			}
			if(isset($record))
			{
				$xsd=explode('.',$dq_shoplist['q_end_time']);
				if(count($xsd)>1) $xsd=$xsd[1];
				else $xsd=$xsd[0];
				$data['top'][0]['q_end_time_data']=date('Y-m-d H:i:s',$dq_shoplist['q_end_time']).'.'.$xsd;
				$xsd=explode('.',$record[0]['time']);
				if(count($xsd)>1) $xsd=$xsd[1];
				else $xsd=$xsd[0];
				$data['top'][0]['qg_time_data']=date('Y-m-d H:i:s',$record[0]['time']).'.'.$xsd;
				$data['top'][0]['q_end_time']=$dq_shoplist['q_end_time'];
				$data['top'][0]['uid']=$dq_shoplist['q_uid'];
				$data['top'][0]['code']=$dq_shoplist['q_user_code'];
				$data['top'][0]['gonumber']=$record[0]['num'];
				$data['top'][0]['qg_time']=$record[0]['time'];
				$data['top'][0]['ip']=$record[0]['ip'];
				$data['top'][0]['userName']=$users['userName'];
				$data['top'][0]['userPhoto']=$users['userPhoto'];
			}
			else $data['top']=[];
		}
		else $data['top']=[];
		
		if($dq_shoplist['qishu']!=1)
		{
			$sql="select s.id,s.sid,s.shop_id,s.title,s.qishu,s.thumb,s.canyurenshu,s.zongrenshu,s.shenyurenshu,s.yunjiage,s.money,s.q_end_time,s.q_showtime,q_uid,q_user_code from `".$oto."_shoplist` as s where sid='{$sid}' and qishu='".($qishu-1)."'";
			$result=$db->query($sql);
			while($row=$result->fetch_assoc()){
				$top_shoplist=$row;
			}
			$q_uid=-1;
			if(isset($top_shoplist))
			{
				if($top_shoplist['q_end_time']!=null && $top_shoplist['q_showtime']='N')
				{
					$q_uid=$top_shoplist['q_uid'];
				}
			}
			$sql="select sum(`gonumber`) as num,`time`,username,uphoto,ip from `".$oto."_member_go_record` as r where shopid='".$top_shoplist['id']."' and uid='{$q_uid}' order by `time` desc";
			$result=$db->query($sql);
			while($row=$result->fetch_assoc()){
				$top_record[]=$row;
			}
			$sql="select * from `".$oto."_users` as r where userId='{$q_uid}'";
			$result=$db->query($sql);
			while($row=$result->fetch_assoc()){
				$users2=$row;
			}
			if(isset($top_record))
			{
				$xsd=explode('.',$top_shoplist['q_end_time']);
				if(count($xsd)>1) $xsd=$xsd[1];
				else $xsd=$xsd[0];
				$data['bottom'][0]['q_end_time_data']=date('Y-m-d H:i:s',$top_shoplist['q_end_time']).'.'.$xsd;
				$xsd=explode('.',$top_record[0]['time']);
				if(count($xsd)>1) $xsd=$xsd[1];
				else $xsd=$xsd[0];
				$data['bottom'][0]['qg_time_data']=date('Y-m-d H:i:s',$top_record[0]['time']).'.'.$xsd;
				$data['bottom'][0]['q_end_time']=$top_shoplist['q_end_time'];
				$data['bottom'][0]['uid']=$top_shoplist['q_uid'];
				$data['bottom'][0]['code']=$top_shoplist['q_user_code'];
				$data['bottom'][0]['gonumber']=$top_record[0]['num'];
				$data['bottom'][0]['qg_time']=$top_record[0]['time'];
				$data['bottom'][0]['ip']=$top_record[0]['ip'];
				$data['bottom'][0]['userName']=$users2['userName'];
				$data['bottom'][0]['userPhoto']=$users2['userPhoto'];
			}
			else $data['bottom']=[];
		}
		else $data['bottom']=[];
		echo json_encode($data);
?>